//////////////////////////////////////////////////////////////////////////

#ifndef _ALLRESEARCHESDIALOG_H_
#include "allresearchesdialog.h"
#endif

//////////////////////////////////////////////////////////////////////////

#include "ui_allresearchesdialog.h"

#ifndef _MAINWINDOW_H_
#include "mainwindow.h"
#endif

#include <QMessageBox>
#include <QStringListModel>

//////////////////////////////////////////////////////////////////////////


AllResearchesDialog::AllResearchesDialog( QWidget* _parent )
: Dialog(_parent),
  m_ui(new Ui::AllResearchesDialog)
{
    m_ui->setupUi(this);

    connect( m_ui->m_NewResearchButton, SIGNAL(clicked()), this, SLOT(NewResearch()) );
    connect( m_ui->m_OpenResearchButton, SIGNAL(clicked()), this, SLOT(OpenResearch()) );
    connect( m_ui->m_DeleteResearchButton, SIGNAL(clicked()), this, SLOT(DeleteResearch()) );
}


//////////////////////////////////////////////////////////////////////////


AllResearchesDialog::~AllResearchesDialog()
{
    delete m_ui;
}


//////////////////////////////////////////////////////////////////////////


void AllResearchesDialog::NewResearch()
{
}


//////////////////////////////////////////////////////////////////////////


void AllResearchesDialog::OpenResearch()
{
    // Get selection index
    QModelIndex modelIndex = m_ui->m_AllResearchesView->currentIndex();
    if ( !modelIndex.isValid() )
    {
        QMessageBox box(QMessageBox::Warning, tr("Error"),
            tr("Please, select research."),
            QMessageBox::Ok, this);

        box.exec();
        return;
    }

    // Get number of selected row
    size_t row = modelIndex.row();

    // Get selected research
    const Research& research = GetMainWindow()->GetCurrPatient().GetPatientResearches()[row];

    // Set current research
    GetMainWindow()->GetCurrPatient().SetCurrentResearch(&research);

    // Show it
    GetMainWindow()->ShowCurrentResearch();
}


//////////////////////////////////////////////////////////////////////////


void AllResearchesDialog::DeleteResearch()
{

}


//////////////////////////////////////////////////////////////////////////


void AllResearchesDialog::show()
{
    QAbstractItemModel* model = m_ui->m_AllResearchesView->model();
    if (model)
    {
        m_ui->m_AllResearchesView->setModel(NULL);
        delete model;
    }

    GetMainWindow()->GetDatabase().FetchPatientResearches( GetMainWindow()->GetCurrPatient() );
    const Patient::Researches& researches = GetMainWindow()->GetCurrPatient().GetPatientResearches();

    QStringList results;
    for ( size_t i = 0; i < researches.size(); ++i)
        results << researches[i].GetDate();        

    m_ui->m_AllResearchesView->setModel( new QStringListModel(results) );

    // If any patient were found -- select first
    if ( !researches.empty() )
        m_ui->m_AllResearchesView->setCurrentIndex( m_ui->m_AllResearchesView->model()->index(0, 0) );

    QDialog::show();
}


//////////////////////////////////////////////////////////////////////////
